package com.example.shop.user.dto;

import com.example.shop.pojo.User;
import com.example.shop.user.enums.Sex;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.uuid.Generators;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.*;
import java.io.File;
import java.time.LocalDate;

@Data
public class RegisterParam {
    /**
     * 用户头像
     */
    private MultipartFile avatarFile;
    /**
     * 如果注册带有face图片，说明该用户开启人脸识别功能
     */
    private MultipartFile faceFile;
    /**
     * 用户名
     */
    @Size(max = 255)
    private String userName;
    /**
     * 性别
     */
    @NotNull
    private Sex userSex;
    /**
     * 所在地区
     */
    @Size(max = 255)
    private String userArea;
    @JsonDeserialize(using = LocalDateDeserializer.class)
    @JsonSerialize(using = LocalDateSerializer.class)
    @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GMT+8")
    /**
     * 出生日期
     */
    private LocalDate userDateBirth;
    @NotEmpty
    /**
     * 用户账户
     */
    @Size(max = 255)
    private String userAccount;
    /**
     * 密码
     */
    @Size(max = 255)
    private String userPassword;

    public User toUser(){
        User user = new User();
        user.setUserId(Generators.timeBasedGenerator().generate().node());
        user.setUserName(userName);
        user.setUserSex(userSex.getSex());
        user.setUserArea(userArea);
        user.setUserDateBirth(userDateBirth);
        user.setUserAccount(userAccount);
        user.setUserPassword(userPassword);
        user.setUserResourceDir(User.RESOURCE_ROOT_DIR + userAccount + File.separator);
        return user;
    }
}
